<?php
/*
 * 推送消息模块
 */
 namespace Admin\Controller;
 use Think\Controller;
 require_once '/workerman/GatewayClient/Gateway.php';
 use GatewayClient\Gateway;
 class PushController extends CommonController {
 	public function index(){
 		$this->display(); 
 	}
 	
// 	扫码推送测试
 	public function add(){
 		$data = $_POST;
 		$msg = array(
 			"type"=>"send",
 			"content"=>$data['msg'],
 			"client_id"=>$data['client_id']
 		);
 		Gateway::$registerAddress = '127.0.0.1:1236';
 		
 		$new_message = array(
            'type'=>'scan', 
            'from_client_id'=>$client_id,
            'from_client_name' =>'admin',
            'to_client_id'=>$msg['client_id'],
            'content'=>"扫码成功！请在APP确认登录！",
            'time'=>date('Y-m-d H:i:s'),
        );
 		Gateway::sendToClient($msg['client_id'], json_encode($new_message));
// 		Gateway::sendToGroup(1 ,json_encode($new_message));
 		return show(1,$data['msg'],$ret);
 	}
 	
// 	扫码事件
 	public function scan () {
 		$data = $_POST;
 		$new_message = array(
 			'type'=>'scan', 
            'from_client_id'=>$data['uid'],
            'from_client_name' =>$data['uid'],
            'to_client_id'=>$data['uid'],
            'content'=>"扫码成功！请在APP确认登录！",
            'time'=>date('Y-m-d H:i:s'),
 		);
		Gateway::sendToUid($data['uid'], json_encode($new_message));
// 		Gateway::sendToClient($msg['client_id'], json_encode($new_message));
 		return show(1,$data['content']);
 	}
 	
 	public function login () {
// 		验证登录逻辑
		$username = $_POST['username'];
		$password = $_POST['password'];
		$ret = D('Admin')->getAdminByUsername($username);
		if(!$ret){
				return show(0,'用户不存在');
		}
		if($ret['password']!=getMd5Password($password)){
			return show(0,'密码不正确');
		}
		
		$new_message = array(
 			'type'=>'login_success', 
            'from_client_id'=>$_POST['uid'],
            'from_client_name' =>$_POST['uid'],
            'to_client_id'=>$_POST['uid'],
            'content'=>"扫码成功！请在APP确认登录！",
            'time'=>date('Y-m-d H:i:s'),
 		);
		
		session('adminUser',$ret);
		Gateway::sendToUid($_POST['uid'], json_encode($new_message));
		return show(1, '登录成功！');
 	}
 }
 